<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetNotePraiseCount extends PageBase
{

    private $objNote;
    private $objNotePraise;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNotePraise = new NotePraise();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'note_id' => 'require',
        ];
        $msg = [
            'note_id.require' => '帖子id不能为空',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {
        $this->checkToken($param);

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            throw new Exception($ret, Error::ERR_PARAM);
        }

        // 2、从数据库中获取帖子详情
        $ret = $this->getNoteInfoFromDB($param['note_id']);
        if (false === $ret) {
            throw new Exception('get note info from db fail. [note_id: ' . $param['note_id'] . ']', Error::ERR_SYS);
        }

        if (empty($ret)) {
            throw new Exception('note info is empty. [note_id: ' . $param['note_id'] . ']', Error::ERR_NOTE_NOT_EXIST);
        }

        $notePriase = $this->getNotePraiseFromDB($param['note_id']);

        $this->data['praise_count'] = empty($notePriase) ? 0 : count($notePriase);
    }

    /**
     * 从数据库中获取帖子详情
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getNoteInfoFromDB($note_id)
    {
        if (!$note_id) {
            return false;
        }
        $arrCond = [
            'id' => ['=', $note_id]
        ];
        $ret = $this->objNote->getNote($arrCond);
        if (false === $ret) {
            return false;
        }
        if (empty($ret)) {
            return [];
        }
        $ret = $ret[0];
        return $ret;
    }

    /**
     * 从数据库中获取帖子点赞
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getNotePraiseFromDB($note_id)
    {
        if (!$note_id) {
            return false;
        }
        $arrCond = [
            'note_id' => ['=', $note_id],
            'is_delete' => ['=', Flag::CONST_NOTEPRIASE_DELETE_NOT]
        ];
        $ret = $this->objNotePraise->getNotePraise($arrCond, ['userid']);
        if (false === $ret) {
            return false;
        }
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }
}
